//
// Created by gastonfeng on 2022/9/23.
//
typedef unsigned char u8;
typedef unsigned short u16;

u16 crc_16(u8 *buf, int cnt, u16 init) {
    u16 temp;
    u16 flag;

    temp = init;

    while (cnt-- != 0) {
        temp = temp ^ *buf++;
        for (int bit = 1; bit <= 8; bit++) {
            flag = temp & 0x0001;
            /* NOTE:
             *  - since temp is unsigned, we are guaranteed a zero in MSbit;
             *  - if it were signed, the value placed in the MSbit would be
             *      compiler dependent!
             */
            temp >>= 1;
            if (flag)
                temp = temp ^ 0xA001;
        }
    }
    return temp;
}